#include <dspSimulator/instruction/packl4.h>


bool PACKL4::checkCondition(std::shared_ptr<DspMachine> dspMachine) {
    return checkStandardCondition(dspMachine);
}

void PACKL4::loadInstIRIntoJit(llvm::LLVMContext *llvmContext, llvm::Module *module,
                               std::shared_ptr<llvm::IRBuilder<>> irBuilder) {
    llvm::FunctionType *funcType = llvm::FunctionType::get(irBuilder->getVoidTy(), false);
    // ---------------------------------PACKL4 (.unit) i4, xi4, i4 ---------------------------------------------
    {
        llvm::Function *packl4L = llvm::Function::Create(funcType, llvm::GlobalValue::ExternalLinkage, "PACKL4Kind1",
                                                         module);
        llvm::BasicBlock *entryBB = llvm::BasicBlock::Create(*llvmContext, "entry", packl4L);
        irBuilder->SetInsertPoint(entryBB);

        auto dstRegPtr = getDstLoRegPtr(module, irBuilder);

        auto src2RegPtr = getSrc2LoRegPtr(module, irBuilder);
        auto src2Reg = irBuilder->CreateLoad(src2RegPtr);
        auto a_2 = irBuilder->CreateAnd(src2Reg, irBuilder->getInt32(0x00ff0000));
        auto a_0 = irBuilder->CreateAnd(src2Reg, irBuilder->getInt32(0x000000ff));

        auto src1RegPtr = getSrc1LoRegPtr(module, irBuilder);
        auto src1Reg = irBuilder->CreateLoad(src1RegPtr);
        auto b_2 = irBuilder->CreateAnd(src1Reg, irBuilder->getInt32(0x00ff0000));
        auto b_0 = irBuilder->CreateAnd(src1Reg, irBuilder->getInt32(0x000000ff));

        auto a_2Dst = irBuilder->CreateShl(a_2, irBuilder->getInt32(8));
        auto a_0Dst = irBuilder->CreateShl(a_0, irBuilder->getInt32(16));
        auto b_2Dst = irBuilder->CreateLShr(b_2, irBuilder->getInt32(8));

        auto temp1 = irBuilder->CreateOr(a_2Dst, a_0Dst);
        auto temp2 = irBuilder->CreateOr(b_2Dst, b_0);

        auto res = irBuilder->CreateOr(temp1, temp2);
        irBuilder->CreateStore(res, dstRegPtr);
        irBuilder->CreateRetVoid();
    }
}

std::shared_ptr<Instruction> PACKL4::decode(std::shared_ptr<InstInfo> instInfo, u64 baseCycle) {
    std::shared_ptr<PACKL4> packl4;
    auto dspInstInfo = std::dynamic_pointer_cast<DspInstInfo>(instInfo);
    bool isStandardInst = dspInstInfo->isStandardInstruction();
    if (isStandardInst) {
        auto data = vectorToBitSet<32>(dspInstInfo->getBits());
        if (checkBits<10>(data, 2, 0b1101000110) && existStandardCondition(data)) {
            packl4 = std::make_shared<PACKL4>(dspInstInfo->getAddress(), baseCycle);
            packl4->creg_z = extractBits<4>(data, 28);
            packl4->dst = extractBits<5>(data, 23).to_ulong();
            packl4->src2 = extractBits<5>(data, 18).to_ulong();
            packl4->src1 = extractBits<5>(data, 13).to_ulong();
            packl4->x = data[12];
            packl4->s = data[1];
            packl4->p = data[0];
            if (packl4->s == 0) {
                packl4->funcUnit = FuncUnit::L1;
            } else {
                packl4->funcUnit = FuncUnit::L2;
            }
            packl4->instFormat = InstFormat::Kind1;
        }
    }
    return packl4;
}

void PACKL4::executeCustom(std::shared_ptr<TargetMachine> targetMachine, ExecutePhase executePhase) {

}

std::shared_ptr<DspInstruction> PACKL4::clone() {
    return std::make_shared<PACKL4>(*this);
}

std::string PACKL4::toString() const {
    std::string res;
    res += getStandardConditionString() + " " + m_name + " " + FuncUnitName[static_cast<u32>(funcUnit)] + " ";
    if (instFormat == InstFormat::Kind1) {
        res += getSrc1Reg32Name() + "," + getSrc2Reg32Name() + "," + getDstReg32Name();
    }
    return res;
}
